package com.txd.mofa.fenlei.fgt;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.R;
import com.txd.mofa.adapter.FragmentAdp;
import com.txd.mofa.base.BaseFgt;
import com.txd.mofa.bean.FenleiBean;
import com.txd.mofa.bean.FlagBean;
import com.txd.mofa.net.NetUtils;
import com.txd.mofa.view.CustomViewpager;
import com.txd.mofa.view.HXLinePagerIndicator;
import com.txd.mofa.view.MyViewPager;
import com.txd.mofa.view.ScaleTransitionPagerTitleView;
import com.txd.mofa.xiangce.models.puzzle.Line;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Layout(R.layout.fgt_sousuo)
public class SousuoFgt extends BaseFgt {

    CustomViewpager viewpager;
    private MagicIndicator magicIndicator;
    private LinearLayout ll_net;
    private NestedScrollView sc;

    private List<String> listTitle = new ArrayList<>();
    private List<Fragment> list = new ArrayList<>();


    public static SousuoFgt newInstance() {

        Bundle args = new Bundle();

        SousuoFgt fragment = new SousuoFgt();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initViews() {
        super.initViews();

        EventBus.getDefault().register(this);
        sc = findViewById(R.id.sc);
        ll_net = findViewById(R.id.ll_net);
        magicIndicator = findViewById(R.id.magic_indicator);
        viewpager = findViewById(R.id.viewpager);

        http();

    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

        ll_net.setVisibility(View.GONE);

        http();
    }

    @Override
    protected void onNetworkDisConnected() {
        if (flag == false) {
            ll_net.setVisibility(View.VISIBLE);
        }

    }

    private int flag1 = 0;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void fenlei(FenleiBean fenleiBean){

        flag1 = Integer.parseInt(fenleiBean.getString());

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        Huoqu();

    }

    private void http() {

        if (!NetUtils.isNetworkAvailable(me)){
            ll_net.setVisibility(View.VISIBLE);
        }

        HttpRequest.POST(me, categoryList, new Parameter(), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {

                if (error == null) {
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    if (map.get("code").equals("1")){
                        ArrayList<Map<String, String>> data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));

                        listTitle.clear();
                        for (int i = 0; i < data.size(); i++) {
                            listTitle.add(data.get(i).get("title"));
                        }
                        list.clear();
                        for (int i = 0; i < listTitle.size(); i++) {
                            list.add(FenleiFgt.newInstance(i+""));
                        }

                        FragmentAdp fragmentAdp = new FragmentAdp(getChildFragmentManager(), list, listTitle);
                        viewpager.setAdapter(fragmentAdp);
                        viewpager.setOffscreenPageLimit(list.size());

                        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {
                                viewpager.resetHeight(position);
                                sc.scrollTo(0,0);
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {

                            }
                        });

                        CommonNavigator commonNavigator = new CommonNavigator(me);

                        //true 选项卡均分父布局宽度。这种情况仅仅在选项卡较少且文字比较短时候合适
                        //     如果文字很长，且选项卡很多，那么久会挤在一起密密麻麻，视觉上比较难看。
                        //通常设置false。
                        commonNavigator.setAdjustMode(false);

                        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
                            @Override
                            public int getCount() {
                                return listTitle == null ? 0 : listTitle.size();
                            }

                            @Override
                            public IPagerTitleView getTitleView(Context context, final int index) {
//                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context); //使用自定义
                                simplePagerTitleView.setText(listTitle.get(index));
                                simplePagerTitleView.setTextSize(19);
                                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.hui));
                                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.huang));
                                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        viewpager.setCurrentItem(index);
                                    }
                                });

                                return simplePagerTitleView;
                            }

                            @Override
                            public IPagerIndicator getIndicator(Context context) {
                                HXLinePagerIndicator indicator = new HXLinePagerIndicator(context);
                                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                                indicator.setLineHeight(UIUtil.dip2px(context, 4));
                                indicator.setLineWidth(UIUtil.dip2px(context, 20));
                                indicator.setRoundRadius(UIUtil.dip2px(context, 3));

                                indicator.setStartInterpolator(new AccelerateInterpolator());
                                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));

                                return indicator;
                            }
                        });

                        magicIndicator.setNavigator(commonNavigator);

                        ViewPagerHelper.bind(magicIndicator, viewpager);

                        magicIndicator.onPageSelected(flag1);
                        viewpager.setCurrentItem(flag1);

                    }

                }else {
                    toast("网络异常");
                }
            }
        });
    }

    @Override
    public void onLazyLoad() {

    }

    @Override
    public void setEvents() {

    }
}
